package com.yiren.dbaa.module.practice_set.carmodel.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.carmodel.bean.CarBrandBean;
import com.yiren.dbaa.module.practice_set.carmodel.contract.AddCarContract;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaLocationEnum;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaTypeEnum;
import com.yiren.dbaa.module.practice_set.carmodel.presenter.AddCarPresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/5 9:45
 * @description: 共享车模
 */
public class AddCarModelActivity extends BaseActivity implements AddCarContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.sp_brand)
    Spinner spBrand;
    @BindView(R.id.sp_series)
    Spinner spSeries;
    @BindView(R.id.et_start_year)
    EditText etStartYear;
    @BindView(R.id.et_end_year)
    EditText etEndYear;
    @BindView(R.id.sp_antenna_type)
    Spinner spAntennaType;
    @BindView(R.id.sp_antenna_location)
    Spinner spAntennaLocation;
    @BindView(R.id.et_direction)
    EditText etDirection;
    @BindView(R.id.et_direction_value)
    EditText etDirectionValue;
    @BindView(R.id.et_position)
    EditText etPosition;
    @BindView(R.id.et_position_value)
    EditText etPositionValue;

    private AddCarContract.Presenter addCarPresenter;
    /*** 车辆品牌集合 */
    private List<CarBrandBean> carBrandBeans;
    /*** 默认选择的车辆品牌 */
    private int carBrandIndex = 0;
    /*** 车辆品牌系列数组 */
    private String[] carSeries;
    /*** 默认选择的车辆系列 */
    private int carSeriesIndex = 0;
    /*** 天线类型枚举 */
    AntennaTypeEnum[] antennaTypeEnums;
    /*** 默认天线类型 */
    private int antennaTypeIndex = 0;
    /*** 天线摆放位置枚举 */
    private AntennaLocationEnum[] antennaLocationEnums;
    /*** 默认天线摆放位置类型 */
    private int antennaLocationIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_car_model);
        ButterKnife.bind(this);
        initData();
        loadCarBrand();
    }

    private void initData() {
        tvTitle.setText("采集新车模");
        addCarPresenter = new AddCarPresenter(this);
        antennaTypeEnums = AntennaTypeEnum.values();
        antennaLocationEnums = AntennaLocationEnum.values();
        setAntennaTypeSpinner();
        setAntennaLocationSpinner();
    }

    private void loadCarBrand() {
        addCarPresenter.getAllCarBrand(this);
    }

    @Override
    public void showCarBrand(List<CarBrandBean> carBrandBeans) {
        this.carBrandBeans = carBrandBeans;
        carSeries = carBrandBeans.get(carBrandIndex).getSeries().split(",");
        setCarBrandSpinner();
        setCarSeriesSpinner();
    }

    /**
     * 设置车辆品牌下拉框
     */
    private void setCarBrandSpinner() {
        String[] carBrands = new String[carBrandBeans.size()];
        for (int i = 0; i < carBrandBeans.size(); i++) {
            carBrands[i] = carBrandBeans.get(i).getBrand();
        }
        ArrayAdapter<String> carBrandAdapter = new ArrayAdapter<String>(this, R.layout.item_spinner, carBrands);
        carBrandAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spBrand.setAdapter(carBrandAdapter);
        spBrand.setSelection(carBrandIndex);
        spBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                carBrandIndex = position;
                carSeriesIndex = 0;
                carSeries = carBrandBeans.get(carBrandIndex).getSeries().split(",");
                setCarSeriesSpinner();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    /**
     * 设置车系下拉框
     */
    private void setCarSeriesSpinner() {
        if (null != carSeries || carSeries.length != 0) {
            ArrayAdapter<String> carSeriesAdapter = new ArrayAdapter<String>(this, R.layout.item_spinner, carSeries);
            carSeriesAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
            spSeries.setAdapter(carSeriesAdapter);
            spSeries.setSelection(carSeriesIndex);
            spSeries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    carSeriesIndex = position;
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
        }
    }

    /**
     * 设置天线类型下拉框
     */
    private void setAntennaTypeSpinner() {
        String[] antennaTypes = new String[antennaTypeEnums.length];
        for (int i = 0; i < antennaTypeEnums.length; i++) {
            antennaTypes[i] = antennaTypeEnums[i].getDescription();
        }
        ArrayAdapter<String> antennaTypeAdapter = new ArrayAdapter<String>(this, R.layout.item_spinner, antennaTypes);
        antennaTypeAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spAntennaType.setAdapter(antennaTypeAdapter);
        spAntennaType.setSelection(antennaTypeIndex);
        spAntennaType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                antennaTypeIndex = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    /**
     * 设置天线摆放位置下拉框
     */
    private void setAntennaLocationSpinner() {
        String[] antennaLocations = new String[antennaLocationEnums.length];
        for (int i = 0; i < antennaLocationEnums.length; i++) {
            antennaLocations[i] = antennaLocationEnums[i].getDescription();
        }
        ArrayAdapter<String> antennaLocationAdapter = new ArrayAdapter<String>(this, R.layout.item_spinner, antennaLocations);
        antennaLocationAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spAntennaLocation.setAdapter(antennaLocationAdapter);
        spAntennaLocation.setSelection(antennaLocationIndex);
        spAntennaLocation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                antennaLocationIndex = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_start_collect})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_start_collect:
                String startYear = etStartYear.getText().toString().trim();
                if (StringUtils.isEmpty(startYear)) {
                    MessageBox.show(this, "请输入开始年份");
                    return;
                }
                String endYear = etEndYear.getText().toString().trim();
                if (StringUtils.isEmpty(endYear)) {
                    MessageBox.show(this, "请输入结束年份");
                    return;
                }
                String antennaDirection = etDirection.getText().toString().trim();
                String antennaDirectionValue = etDirectionValue.getText().toString().trim();
                if (StringUtils.isEmpty(antennaDirection) || StringUtils.isEmpty(antennaDirectionValue)) {
                    MessageBox.show(this, "请输入定向天线摆放值");
                    return;
                }
                String antennaPosition = etPosition.getText().toString().trim();
                String antennaPositionValue = etPositionValue.getText().toString().trim();
                if (StringUtils.isEmpty(antennaPosition) || StringUtils.isEmpty(antennaPositionValue)) {
                    MessageBox.show(this, "请输入定位天线摆放值");
                    return;
                }
                CarModelEntity carModel = new CarModelEntity();
                carModel.setId(StringUtil.getUUID());
                carModel.setName(carBrandBeans.get(carBrandIndex).getBrand() + "_" + carSeries[carSeriesIndex] + "(" + startYear + "-" + endYear + ")");
                carModel.setAntennaType(antennaTypeIndex);
                carModel.setPositioned(antennaLocationIndex);
                carModel.setUserId(LoadConfigApi.loadUserParamConfig(this).getUserInfo().getId());
                carModel.setCarModelVer(0.0f);
                carModel.setUseRange(2);
                carModel.setUpdateDetails("首次发布");
                carModel.setUpdateMode(2);
                carModel.setPublishTime(System.currentTimeMillis());
                carModel.setAntennaDirection(antennaDirection);
                carModel.setAntennaDirectionValue(Integer.parseInt(antennaDirectionValue));
                carModel.setAntennaPosition(antennaPosition);
                carModel.setAntennaPositionValue(Integer.parseInt(antennaPositionValue));
                if (UserDbController.getInstance(false).insertOrReplace(carModel)) {
                    Intent intent = new Intent(this, CarModelCollectActivity.class);
                    intent.putExtra("carModel", carModel);
                    startActivity(intent);
                } else {
                    MessageBox.show(this, "采集失败，请重试！");
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}